import {
    ysSearch
} from "../../utils/config";
import store from "../../utils/factory";

let that = null;
//获取应用实例
const app = getApp();

Page({
    data: {
        inputVal: "",
        historyList: [],
        hotList: [],
        note: [],
        page: 1,
        searchView: true,
        count: 0,
        finish: false,
        searchStatus: true
    },
    //事件处理函数
    inputTyping: function (e) {
        let val = e.detail.value;
        that.setData({
            inputVal: val
        });
        if (!val) {
            that.setData({
                searchView: true
            });
        }
    },
    onLoad: function () {
        that = this;
    },
    onShow() {
        that.setData({
            finish: false,
            searchView: true
        });
    },
    appointment(e) {
        const obj = e.currentTarget.dataset.obj;
        console.log(obj)
        if (!store.get("userInfo")) {
            wx.navigateTo({
                url: "../autoLogin/autoLogin" // 页面 B
            });
            return false;
        }
        store.set("ysDetail", obj)
        wx.navigateTo({
            url: `../submitOrder/submitOrder`
        });
    },
    onReachBottom() {
        console.log("bottom")
        if (that.data.finish) {
            return false
        }
        that.setData({
            page: that.data.page + 1
        })
        that.search(this.data.inputVal);
    },
    confirmInput(e) {
        let val = this.data.inputVal;
        if (!val) {
            return false;
        }
        if (!that.data.searchStatus) {
            return false
        }
        that.setData({
            searchStatus: false
        })
        let list = store.get("historyList") || [];
        list.push({
            name: val
        });
        store.set("historyList", list);
        this.setData({
            finish: false,
            page: 1,
            note: []
        })
        that.search(val);
    },
    search(value) {
        ysSearch({
            page: this.data.page,
            limit: 10,
            name: value
        }).then(res => {
            that.setData({
                searchStatus: true
            })
            let req = res.data;
            if (req && req.length < 10) {
                that.setData({
                    finish: true
                });
            } else {
                that.setData({
                    finish: false
                });
            }
            req.map(item => {
                item.star = parseInt(item.star);
            })
            if (that.data.page == 1) {
                that.setData({
                    note: []
                })
            }
            let a = [...this.data.note, ...req]
            that.setData({
                searchView: false,
                note: a,
            });
        });
    },
    focusOn(e) {
        if (that.data.note.length > 0) {
            return false
        }
        that.setData({
            searchView: true
        });
        console.log("focus", e);
    },
    getYsDetail(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `../logs/logs?id=${id}`
        });
    },
    goDetail(e) {
        const id = e.currentTarget.dataset.id;
        console.log(id);
        wx.navigateTo({
            url: `../logs/logs?id=${id}`
        });
    },
    clear() {
        store.remove("historyList");
        that.setData({
            historyList: []
        })
    }
});